package com.huawei.esdk.tp.device.terminal;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.log4j.Logger;

import com.huawei.esdk.platform.common.SDKErrorCode;
import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.ThreadLocalHolder;
import com.huawei.esdk.platform.common.bean.commu.BaseJsonBean;
import com.huawei.esdk.platform.common.config.ConfigManager;
import com.huawei.esdk.platform.common.constants.ESDKConstant;
import com.huawei.esdk.platform.common.utils.ApplicationContextUtil;
import com.huawei.esdk.platform.commu.itf.IProtocolAdapterManager;
import com.huawei.esdk.platform.commu.itf.ISDKProtocolAdapter;
import com.huawei.esdk.platform.exception.ProtocolAdapterException;
import com.huawei.esdk.platform.nemgr.base.MultiConnDeviceBase;
import com.huawei.esdk.platform.nemgr.itf.IDeviceConnection;
import com.huawei.esdk.tp.bean.terminal.ReqChangeVideoOutSrcBean;
import com.huawei.esdk.tp.bean.terminal.ReqCtrlCameraOpeateBean;
import com.huawei.esdk.tp.bean.terminal.ReqDirectBroadBean;
import com.huawei.esdk.tp.bean.terminal.ReqRecordBroadBean;
import com.huawei.esdk.tp.bean.terminal.ReqSetLocalMainAuxBean;
import com.huawei.esdk.tp.bean.terminal.ResBean;
import com.huawei.esdk.tp.bean.terminal.ResError;
import com.huawei.esdk.tp.bean.terminal.ResGetConfCtrlPsdBean;
import com.huawei.esdk.tp.bean.terminal.ResGetLocalCamListStateBean;
import com.huawei.esdk.tp.bean.terminal.ResGetMainVideoSourceListBean;
import com.huawei.esdk.tp.bean.terminal.ResGetSubVideoSourceListBean;
import com.huawei.esdk.tp.bean.terminal.ResIsConnectAuxSourceBean;
import com.huawei.esdk.tp.bean.terminal.ResIsReceiveRemAuxStrmBean;
import com.huawei.esdk.tp.bean.terminal.ResIsSendAuxStreamBean;
import com.huawei.esdk.tp.bean.terminal.ResIsSupportReocrdBean;
import com.huawei.esdk.tp.bean.terminal.ResQueryDirectBroadStateBean;
import com.huawei.esdk.tp.bean.terminal.ResQuerySiteVersionInfoBean;
import com.huawei.esdk.tp.bean.terminal.WebComboboxContent;
import com.huawei.esdk.tp.common.ErrInfo;
import com.huawei.esdk.tp.common.constants.InfoKeys;
import com.huawei.esdk.tp.common.constants.TPConstant;
import com.huawei.esdk.tp.domain.model.Site;
import com.huawei.esdk.tp.domain.model.SiteInConference;
import com.huawei.esdk.tp.domain.model.bean.CameraControl;
import com.huawei.esdk.tp.domain.model.bean.ControlPwd;
import com.huawei.esdk.tp.domain.model.bean.VideoSourcesInfo;
import com.huawei.esdk.tp.southcommu.TPJsonOverHttpCustProvider;

public class TerminalDevice extends MultiConnDeviceBase implements
        TerminalServiceItf
{
    private static final Logger LOGGER = Logger.getLogger(TerminalDevice.class);

    protected IProtocolAdapterManager protocolAdapterManager = ApplicationContextUtil.getBean("protocolAdapterManager");
    
    /*
     * Service Access Point (URL)
     */
    private String sap;

    protected ISDKProtocolAdapter protocolAdapter;

    protected String getSap()
    {
        return sap;
    }

    protected void setSap(String sap)
    {
        this.sap = sap;
    }

    public TerminalDevice(String sap)
    {
        this.sap = sap;
        protocolAdapter = protocolAdapterManager.getProtocolInstanceByType(ESDKConstant.PROTOCOL_ADAPTER_TYPE_HTTP, sap);
        protocolAdapter.setSdkProtocolAdatperCustProvider(new TPJsonOverHttpCustProvider());
    }

    @Override
    public IDeviceConnection createConnection(String appId, String sap,
            String user, String pwd)
    {
        return new TerminalConnection(protocolAdapter, this, user, pwd);
    }

    @Override
    public String getConnIdFromContext()
    {
        return (String) ThreadLocalHolder.get().getEntities()
                .get(InfoKeys.SDK_SESSION_ID.toString());
    }

    @Override
    public SDKErrorCode startSendAuxStream()
    {
        BaseJsonBean reqJsonBean = new BaseJsonBean();
        SDKErrorCode result = new SDKErrorCode();
        ResBean res = null;
        try
        {
            res = (ResBean) protocolAdapter.syncSendMessage(reqJsonBean,
                    "WEB_StartSendAuxStreamAPI", BaseJsonBean.class.getName());
            errorCodeDecode(res, result);
        }
        catch (ProtocolAdapterException e)
        {
            LOGGER.error("startSendAuxStreamAPI method error", e);
            result.setErrCode(e.getErrorCode());
        }
        return result;
    }

    @Override
    public SDKErrorCode stopSendAuxStream()
    {
        BaseJsonBean reqJsonBean = new BaseJsonBean();
        SDKErrorCode result = new SDKErrorCode();
        ResBean res = null;
        try
        {
            res = (ResBean) protocolAdapter.syncSendMessage(reqJsonBean,
                    "WEB_StopSendAuxStreamAPI", BaseJsonBean.class.getName());
            errorCodeDecode(res, result);
        }
        catch (ProtocolAdapterException e)
        {
            LOGGER.error("stopSendAuxStream method error", e);
            result.setErrCode(e.getErrorCode());
        }

        return result;
    }

    protected void errorCodeDecode(ResBean res, SDKErrorCode result)
    {
        if (TPConstant.TERMINAL_RETURN_SUCCESS == res.getSuccess())
        {
            result.setErrCode(0);
        }
        else
        {
            if (null == res.getError())
            {
                result.setErrCode(ErrInfo.DEVICE_UNKNOWN_ERRORCODE);
            }
            else
            {
                result.setErrCode(res.getError().getCode());
            }
        }
    }

    /**
     * 查看有没有接入辅流输入源 * @see
     * com.huawei.esdk.tp.device.terminal.TerminalServiceItf#
     * isConnectAuxSourceAPI()
     */
    @Override
    public SDKResult<Integer> isConnectAuxSourceAPI()
            throws ProtocolAdapterException
    {
        SDKResult<Integer> result = new SDKResult<Integer>();
        try
        {
            BaseJsonBean reqJsonBean = new BaseJsonBean();
            ResBean resBean = syncSendProtocolMessage(reqJsonBean,
                    "WEB_IsConnectAuxSourceAPI",
                    ResIsConnectAuxSourceBean.class.getName());
            if (TPConstant.TERMINAL_RETURN_SUCCESS == resBean.getSuccess())
            {
                result.setErrCode(0);
                ResIsConnectAuxSourceBean isConn = (ResIsConnectAuxSourceBean) resBean
                        .getDataBean();
                if ("connect".equals(isConn.getIsSrcConnect()))
                {
                    result.setResult(1);
                }
                else if ("unconnect".equals(isConn.getIsSrcConnect()))
                {
                    result.setResult(0);
                }
            }
            else
            {
                ResError error = resBean.getError();
                result.setErrCode(error.getCode());
            }
        }
        catch (ProtocolAdapterException e)
        {
            LOGGER.error("isConnectAuxSourceAPI method error", e);
            result.setErrCode(e.getErrorCode());
        }
        return result;
    }

    /**
     * 查看是否正在发送辅流 * @see com.huawei.esdk.tp.device.terminal.TerminalServiceItf#
     * isSendAuxStreamAPI()
     */
    @Override
    public SDKResult<Integer> isSendAuxStreamAPI()
            throws ProtocolAdapterException
    {
        SDKResult<Integer> result = new SDKResult<Integer>();
        try
        {
            BaseJsonBean reqJsonBean = new BaseJsonBean();
            ResBean resBean = syncSendProtocolMessage(reqJsonBean,
                    "WEB_IsSendAuxStreamAPI",
                    ResIsSendAuxStreamBean.class.getName());
            if (TPConstant.TERMINAL_RETURN_SUCCESS == resBean.getSuccess())
            {
                result.setErrCode(0);
                ResIsSendAuxStreamBean isSend = (ResIsSendAuxStreamBean) resBean
                        .getDataBean();
                if ("auxOpen".equals(isSend.getIsSendAux()))
                {
                    result.setResult(1);
                }
                else if ("auxClose".equals(isSend.getIsSendAux()))
                {
                    result.setResult(0);
                }
            }
            else
            {
                ResError error = resBean.getError();
                result.setErrCode(error.getCode());
            }
        }
        catch (ProtocolAdapterException e)
        {
            LOGGER.error("isSendAuxStreamAPI method error", e);
            result.setErrCode(e.getErrorCode());
        }
        return result;
    }

    /**
     * 查询是否正在接收辅流 * @see com.huawei.esdk.tp.device.terminal.TerminalServiceItf#
     * isReceiveRemAuxStrmAPI()
     */
    @Override
    public SDKResult<Integer> isReceiveRemAuxStrmAPI()
            throws ProtocolAdapterException
    {
        SDKResult<Integer> result = new SDKResult<Integer>();
        try
        {
            BaseJsonBean reqJsonBean = new BaseJsonBean();
            ResBean resBean = syncSendProtocolMessage(reqJsonBean,
                    "WEB_IsReceiveRemAuxStrmAPI",
                    ResIsReceiveRemAuxStrmBean.class.getName());
            if (TPConstant.TERMINAL_RETURN_SUCCESS == resBean.getSuccess())
            {
                result.setErrCode(0);
                ResIsReceiveRemAuxStrmBean isReceive = (ResIsReceiveRemAuxStrmBean) resBean
                        .getDataBean();
                if ("auxOpen".equals(isReceive.getIsSendAux()))
                {
                    result.setResult(1);
                }
                else if ("auxClose".equals(isReceive.getIsSendAux()))
                {
                    result.setResult(0);
                }
            }
            else
            {
                ResError error = resBean.getError();
                result.setErrCode(error.getCode());
            }
        }
        catch (ProtocolAdapterException e)
        {
            LOGGER.error("isSendAuxStreamAPI method error", e);
            result.setErrCode(e.getErrorCode());
        }
        return result;
    }

    /**
     * 查询辅流列表 * @see com.huawei.esdk.tp.device.terminal.TerminalServiceItf#
     * getSubVideoSourceList()
     */
    @Override
    public SDKResult<Map<Integer, String>> getSubVideoSourceList()
            throws ProtocolAdapterException
    {
        SDKResult<Map<Integer, String>> result = new SDKResult<Map<Integer, String>>();
        Map<Integer, String> map = new TreeMap<Integer, String>();
        try
        {
            BaseJsonBean reqJsonBean = new BaseJsonBean();
            ResBean resBean = syncSendProtocolMessage(reqJsonBean,
                    "WEB_GetSubVideoSourceListAPI",
                    ResGetSubVideoSourceListBean.class.getName());
            if (TPConstant.TERMINAL_RETURN_SUCCESS == resBean.getSuccess())
            {
                result.setErrCode(0);
                ResGetSubVideoSourceListBean subVideoBean = (ResGetSubVideoSourceListBean) resBean
                        .getDataBean();
                List<WebComboboxContent> videoList = subVideoBean.getItemList();
                for (int i = 0; i < videoList.size(); i++)
                {
                    map.put(videoList.get(i).getItemID(), videoList.get(i)
                            .getSzItemName());
                }
            }
            else
            {
                ResError error = resBean.getError();
                result.setErrCode(error.getCode());
            }
            result.setResult(map);
        }
        catch (ProtocolAdapterException e)
        {
            LOGGER.error("isSendAuxStreamAPI method error", e);
            result.setErrCode(e.getErrorCode());
        }
        return result;
    }

    /**
     * 查询版本信息 * @see com.huawei.esdk.tp.device.terminal.TerminalServiceItf#
     * querySiteVersionInfo()
     */
    @Override
    public SDKResult<Site> querySiteVersionInfo()
            throws ProtocolAdapterException
    {
        SDKResult<Site> result = new SDKResult<Site>();
        Site site = new Site();
        try
        {
            BaseJsonBean reqJsonBean = new BaseJsonBean();
            ResBean resBean = syncSendProtocolMessage(reqJsonBean,
                    "WEB_GetVersionInfoAPI",
                    ResQuerySiteVersionInfoBean.class.getName());
            if (TPConstant.TERMINAL_RETURN_SUCCESS == resBean.getSuccess())
            {
                ResQuerySiteVersionInfoBean siteVersion = (ResQuerySiteVersionInfoBean) resBean
                        .getDataBean();
                site.setHardVersion(siteVersion.getHardVersion());
                site.setLisence(siteVersion.getLisence());
                site.setLogicVersion(siteVersion.getLogicVersion());
                site.setModel(siteVersion.getModel());
                site.setSoftVersion(siteVersion.getSoftVersion());
                if (siteVersion.getMicVersion().size() > 0)
                {
                    // String micVersion = "";
                    Map<String, String> micVersion = new TreeMap<String, String>();
                    for (int i = 0; i < siteVersion.getMicVersion().size(); i++)
                    {
                        micVersion.put(siteVersion.getMicVersion().get(i)
                                .getIp(), siteVersion.getMicVersion().get(i)
                                .getMicVersion());

                    }
                    site.setMicVersion(micVersion);
                }
            }
            else
            {
                ResError error = resBean.getError();
                result.setErrCode(error.getCode());
            }
            result.setResult(site);
        }
        catch (ProtocolAdapterException e)
        {
            LOGGER.error("querySiteVersionInfo method error", e);
            result.setErrCode(e.getErrorCode());
        }
        return result;
    }

    public ResBean syncSendProtocolMessage(BaseJsonBean reqJsonBean,
            String methodName, String resBeanName)
            throws ProtocolAdapterException
    {
        ResBean res = (ResBean) protocolAdapter.syncSendMessage(reqJsonBean,
                methodName, resBeanName);
        return res;
    }

    @Override
    public SDKErrorCode requestChair()
    {
        BaseJsonBean reqJsonBean = new BaseJsonBean();
        SDKErrorCode result = new SDKErrorCode();
        ResBean res = null;
        try
        {
            res = (ResBean) protocolAdapter.syncSendMessage(reqJsonBean,
                    "WEB_RequestChairAPI", BaseJsonBean.class.getName());

            errorCodeDecode(res, result);
            return result;
        }
        catch (ProtocolAdapterException e)
        {
            LOGGER.error("requestChair method failed", e);
            result.setErrCode(e.getErrorCode());
            return result;
        }
    }

    @Override
    public SDKErrorCode releaseChair()
    {
        BaseJsonBean reqJsonBean = new BaseJsonBean();
        SDKErrorCode result = new SDKErrorCode();
        ResBean res = null;
        try
        {
            res = (ResBean) protocolAdapter.syncSendMessage(reqJsonBean,
                    "WEB_ReleaseChairAPI", BaseJsonBean.class.getName());
            errorCodeDecode(res, result);
            return result;
        }
        catch (ProtocolAdapterException e)
        {
            LOGGER.error("releaseChair method failed", e);
            result.setErrCode(e.getErrorCode());
            return result;
        }
    }

    @Override
    public SDKResult<ControlPwd> getConfCtrlPsd()
    {
        BaseJsonBean reqJsonBean = new BaseJsonBean();
        SDKResult<ControlPwd> result = new SDKResult<ControlPwd>();
        ResBean res = null;
        try
        {
            res = (ResBean) protocolAdapter.syncSendMessage(reqJsonBean,
                    "WEB_GetConfCtrlPsdAPI",
                    ResGetConfCtrlPsdBean.class.getName());

            if (TPConstant.TERMINAL_RETURN_SUCCESS == res.getSuccess())
            {
                ResGetConfCtrlPsdBean resBean = (ResGetConfCtrlPsdBean) res
                        .getDataBean();
                ControlPwd controlPwd = new ControlPwd();
                controlPwd.setHasPwd(resBean.getHasPwd());
                controlPwd.setPwd(resBean.getPwd());

                result.setResult(controlPwd);
            }
            else
            {
                errorCodeDecode(res, result);
            }
            return result;
        }
        catch (ProtocolAdapterException e)
        {
            LOGGER.error("getConfCtrlPsd method failed", e);
            result.setErrCode(e.getErrorCode());
            return result;
        }
    }

    @Override
    public SDKResult<SiteInConference> queryBroadInfo()
    {
        BaseJsonBean reqJsonBean = new BaseJsonBean();
        SDKResult<SiteInConference> result = new SDKResult<SiteInConference>();
        ResBean res = null;
        try
        {
            res = (ResBean) protocolAdapter.syncSendMessage(reqJsonBean,
                    "WEB_QueryDirectBroadStateAPI",
                    ResQueryDirectBroadStateBean.class.getName());

            if (TPConstant.TERMINAL_RETURN_SUCCESS == res.getSuccess())
            {
                ResQueryDirectBroadStateBean resBean = (ResQueryDirectBroadStateBean) res
                        .getDataBean();
                SiteInConference siteInConference = new SiteInConference();
                siteInConference.setDirectBroadStatus(resBean.getUlDirectBroad());
                siteInConference.setRecordBroadStatus(resBean.getUlRecordBroad());

                result.setResult(siteInConference);
            }
            else
            {
                errorCodeDecode(res, result);
            }
            return result;
        }
        catch (ProtocolAdapterException e)
        {
            LOGGER.error("queryBroadInfo method failed", e);
            result.setErrCode(e.getErrorCode());
            return result;
        }
    }

    @Override
    public SDKResult<SiteInConference> isSupportReocrd()
    {
        BaseJsonBean reqJsonBean = new BaseJsonBean();
        SDKResult<SiteInConference> result = new SDKResult<SiteInConference>();
        ResBean res = null;
        try
        {
            res = (ResBean) protocolAdapter.syncSendMessage(reqJsonBean,
                    "WEB_IsSupportReocrdAPI",
                    ResIsSupportReocrdBean.class.getName());

            if (TPConstant.TERMINAL_RETURN_SUCCESS == res.getSuccess())
            {
                ResIsSupportReocrdBean resBean = (ResIsSupportReocrdBean) res
                        .getDataBean();
                SiteInConference siteInConference = new SiteInConference();
                siteInConference.setIsSupportLive(resBean.getIsSupportLive());
                siteInConference.setIsSupportRecord(resBean
                        .getIsSupportRecord());

                result.setResult(siteInConference);
            }
            else
            {
                errorCodeDecode(res, result);
            }
            return result;
        }
        catch (ProtocolAdapterException e)
        {
            LOGGER.error("isSupportReocrd method failed", e);
            result.setErrCode(e.getErrorCode());
            return result;
        }
    }

    @Override
    public SDKErrorCode startRecordBroad()
    {
        ReqRecordBroadBean reqJsonBean = new ReqRecordBroadBean();
        SDKErrorCode result = new SDKErrorCode();
        reqJsonBean.setAction(ConfigManager.getInstance()
                .getValue("professional.common.start_record"));
        ResBean res = null;
        try
        {
            res = (ResBean) protocolAdapter.syncSendMessage(reqJsonBean,
                    "WEB_RecordBroadAPI", BaseJsonBean.class.getName());
            errorCodeDecode(res, result);
            return result;
        }
        catch (ProtocolAdapterException e)
        {
            LOGGER.error("startRecordBroad method failed", e);
            result.setErrCode(e.getErrorCode());
            return result;
        }
    }

    @Override
    public SDKErrorCode stopRecordBroad()
    {
        ReqRecordBroadBean reqJsonBean = new ReqRecordBroadBean();
        reqJsonBean.setAction(ConfigManager.getInstance()
                .getValue("professional.common.stop_record"));
        SDKErrorCode result = new SDKErrorCode();
        ResBean res = null;
        try
        {
            res = (ResBean) protocolAdapter.syncSendMessage(reqJsonBean,
                    "WEB_RecordBroadAPI", BaseJsonBean.class.getName());
            errorCodeDecode(res, result);
            return result;
        }
        catch (ProtocolAdapterException e)
        {
            LOGGER.error("stopRecordBroad method failed", e);
            result.setErrCode(e.getErrorCode());
            return result;
        }
    }

    @Override
    public SDKErrorCode startDirectBroad()
    {
        ReqDirectBroadBean reqJsonBean = new ReqDirectBroadBean();
        reqJsonBean.setAction(ConfigManager.getInstance()
                .getValue("professional.common.start_direct"));
        SDKErrorCode result = new SDKErrorCode();
        ResBean res = null;
        try
        {
            res = (ResBean) protocolAdapter.syncSendMessage(reqJsonBean,
                    "WEB_DirectBroadAPI", BaseJsonBean.class.getName());
            errorCodeDecode(res, result);
            return result;
        }
        catch (ProtocolAdapterException e)
        {
            LOGGER.error("startDirectBroad method failed", e);
            result.setErrCode(e.getErrorCode());
            return result;
        }
    }

    @Override
    public SDKErrorCode stopDirectBroad()
    {
        ReqDirectBroadBean reqJsonBean = new ReqDirectBroadBean();
        reqJsonBean.setAction(ConfigManager.getInstance()
                .getValue("professional.common.stop_direct"));
        SDKErrorCode result = new SDKErrorCode();
        ResBean res = null;
        try
        {
            res = (ResBean) protocolAdapter.syncSendMessage(reqJsonBean,
                    "WEB_DirectBroadAPI", BaseJsonBean.class.getName());
            errorCodeDecode(res, result);
            return result;
        }
        catch (ProtocolAdapterException e)
        {
            LOGGER.error("stopDirectBroad method failed", e);
            result.setErrCode(e.getErrorCode());
            return result;
        }
    }

    @Override
    public SDKResult<List<VideoSourcesInfo>> queryVideoOutSrcState()
    {
        SDKResult<List<VideoSourcesInfo>> result = new SDKResult<List<VideoSourcesInfo>>();
        BaseJsonBean reqJsonBean = new BaseJsonBean();
        try
        {
            ResBean resBean = syncSendProtocolMessage(reqJsonBean,
                    "WEB_GetLocalCamListStateAPI",
                    ResGetLocalCamListStateBean.class.getName());
            if (TPConstant.TERMINAL_RETURN_SUCCESS == resBean.getSuccess())
            {
                result.setResult(TerminalResConvert
                        .videoSourcesInfoListConvert(resBean));
                result.setErrCode(0);
            }
            else
            {
                if (null == resBean.getError())
                {
                    result.setErrCode(ErrInfo.DEVICE_UNKNOWN_ERRORCODE);
                }
                else
                {
                    result.setErrCode(resBean.getError().getCode());
                }
            }
        }
        catch (ProtocolAdapterException e)
        {
            LOGGER.error("queryVideoOutSrcState error", e);
            result.setErrCode(e.getErrorCode());
        }
        return result;
    }

    @Override
    public SDKErrorCode ctrlCameraOpeate(CameraControl ctrlCameraOpeateBean)
    {
        // 定义协议相关的Bean
        ReqCtrlCameraOpeateBean reqJsonBean = new ReqCtrlCameraOpeateBean();
        // B: Domain Bean转成协议相关的Bean
        reqJsonBean.setCamState(ctrlCameraOpeateBean.getCamState());
        reqJsonBean.setCamAction(ctrlCameraOpeateBean.getCamAction());
        reqJsonBean.setCamPos(ctrlCameraOpeateBean.getCamPos());
        reqJsonBean.setCamSrc(ctrlCameraOpeateBean.getCamSrc());

        SDKErrorCode result = new SDKErrorCode();
        ResBean res = null;
        try
        {
            res = syncSendProtocolMessage(reqJsonBean,
                    "WEB_CtrlCameraOpeateAPI", BaseJsonBean.class.getName());
            errorCodeDecode(res, result);
        }
        catch (ProtocolAdapterException e)
        {
            LOGGER.error("ctrlCameraOpeateAPI method error", e);
            result.setErrCode(e.getErrorCode());
        }
        return result;
    }

    @Override
    public SDKErrorCode setLocalMainAux(List<Integer> localMainSrc,
            Integer localAuxSrc)
    {
        SDKErrorCode result = new SDKErrorCode();
        ReqSetLocalMainAuxBean reqJsonBean = new ReqSetLocalMainAuxBean();
        
        reqJsonBean.setLocalAuxSrc(localAuxSrc);
        
        if(1 == localMainSrc.size())
        {
            reqJsonBean.setLocalMainSrc(localMainSrc.get(0));
        }
        else if(2 == localMainSrc.size())
        {
            reqJsonBean.setLocalMainSrc(localMainSrc.get(0));
            reqJsonBean.setLocalMainSrc1(localMainSrc.get(1));
        }
        else if(2 < localMainSrc.size())
        {
            reqJsonBean.setLocalMainSrc(localMainSrc.get(0));
            reqJsonBean.setLocalMainSrc1(localMainSrc.get(1));
            reqJsonBean.setLocalMainSrc2(localMainSrc.get(2));
        }
        
        ResBean resBean = null;
        try
        {
            resBean = syncSendProtocolMessage(reqJsonBean,
                    "WEB_SetLocalMainAuxAPI", BaseJsonBean.class.getName());
            errorCodeDecode(resBean, result);
        }
        catch (ProtocolAdapterException e)
        {
            LOGGER.error("setLocalMainAux method error", e);
            result.setErrCode(e.getErrorCode());
        }
        return result;
    }

    @Override
    public SDKResult<Map<Integer, String>> getMainVideoSourceList()
    {
        SDKResult<Map<Integer, String>> result = new SDKResult<Map<Integer, String>>();
        BaseJsonBean reqJsonBean = new BaseJsonBean();
        try
        {
            ResBean resBean = syncSendProtocolMessage(reqJsonBean,
                    "WEB_GetMainVideoSourceListAPI",
                    ResGetMainVideoSourceListBean.class.getName());
            if (TPConstant.TERMINAL_RETURN_SUCCESS == resBean.getSuccess())
            {
                result.setResult(TerminalResConvert
                        .mainVideoSourceListConvert(resBean));
                result.setErrCode(0);
            }
            else
            {
                if (null == resBean.getError())
                {
                    result.setErrCode(ErrInfo.DEVICE_UNKNOWN_ERRORCODE);
                }
                else
                {
                    result.setErrCode(resBean.getError().getCode());
                }
            }

        }
        catch (ProtocolAdapterException e)
        {
            LOGGER.error("setLocalMainAux method error", e);
            result.setErrCode(e.getErrorCode());
        }
        return result;
    }

    @Override
    public SDKErrorCode changeVideoOutSrc(Integer hdOut, Integer videoSrc)
    {
        SDKErrorCode result = new SDKErrorCode();
        ReqChangeVideoOutSrcBean reqJsonBean = new ReqChangeVideoOutSrcBean();
        reqJsonBean.setHdOut(hdOut);
        String videoSrcTypeString = null;
        switch (videoSrc)
        {
            case 0:
                videoSrcTypeString = ConfigManager.getInstance().getValue("professional.videoSrc.local_main");
                break;
            case 1:
                videoSrcTypeString = ConfigManager.getInstance().getValue("professional.videoSrc.local_aux");
                break;
            case 2:
                videoSrcTypeString = ConfigManager.getInstance().getValue("professional.videoSrc.remote_main");
                break;
            case 3:
                videoSrcTypeString = ConfigManager.getInstance().getValue("professional.videoSrc.remote_aux");
                break;
            default:
                break;
        }
        if(null == videoSrcTypeString)
        {
            result.setErrCode(ErrInfo.PARAM_OUTOFRANGE_ERRORCODE);
            return result;
        }
        reqJsonBean.setVideoSrc(videoSrcTypeString);

        ResBean resBean = null;
        try
        {
            resBean = syncSendProtocolMessage(reqJsonBean,
                    "WEB_ChangeVideoOutSrcAPI", BaseJsonBean.class.getName());
            errorCodeDecode(resBean, result);
        }
        catch (ProtocolAdapterException e)
        {
            LOGGER.error("changeVideoOutSrc method error", e);
            result.setErrCode(e.getErrorCode());
        }
        return result;
    }

    @Override
    public Boolean releaseConns()
    {
        return true;
    }
}
